{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "toc": true
   },
   "source": [
    "<h1>目录<span class=\"tocSkip\"></span></h1>\n",
    "<div class=\"toc\"><ul class=\"toc-item\"><li><span><a href=\"#需求\" data-toc-modified-id=\"需求-1\"><span class=\"toc-item-num\">1&nbsp;&nbsp;</span>需求</a></span></li><li><span><a href=\"#思想\" data-toc-modified-id=\"思想-2\"><span class=\"toc-item-num\">2&nbsp;&nbsp;</span>思想</a></span></li><li><span><a href=\"#实现\" data-toc-modified-id=\"实现-3\"><span class=\"toc-item-num\">3&nbsp;&nbsp;</span>实现</a></span></li><li><span><a href=\"#理解\" data-toc-modified-id=\"理解-4\"><span class=\"toc-item-num\">4&nbsp;&nbsp;</span>理解</a></span></li></ul></div>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 需求\n",
    "```\n",
    "给定一个二叉树，找出其最大深度。\n",
    "\n",
    "二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。\n",
    "\n",
    "说明: 叶子节点是指没有子节点的节点。\n",
    "\n",
    "示例：\n",
    "给定二叉树 [3,9,20,null,null,15,7]，\n",
    "\n",
    "    3\n",
    "   / \\\n",
    "  9  20\n",
    "    /  \\\n",
    "   15   7\n",
    "\n",
    "返回它的最大深度 3 。\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 思想\n",
    "该二叉树的表示形式是:顺序存储\n",
    "1. 定义maxDepth函数(root)\n",
    "1. 传入root节点\n",
    "    1. 如果root节点为空,则说明该节点是叶子节点,没有子节点了,已经到达最大深度,直接返回0\n",
    "    1. 如果root节点不为空,则说明节点还有子节点,将两个子节点作为root传入函数maxDepth,返回两者返回值的最大值\n",
    "1. 这个方法其实是递归的,先到这个树的最深的节点,然后每返回一层,统计层数加1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 实现"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Definition for a binary tree node.\n",
    "# class TreeNode(object):\n",
    "#     def __init__(self, x):\n",
    "#         self.val = x\n",
    "#         self.left = None\n",
    "#         self.right = None\n",
    "\n",
    "class Solution(object):\n",
    "    def maxDepth(self, root):\n",
    "        \"\"\"\n",
    "        :type root: TreeNode\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        if not root:\n",
    "            return 0\n",
    "        return max(self.maxDepth(root.left), self.maxDepth(root.right)) + 1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 理解\n",
    "![手推](二叉树的最大深度.png)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "base"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "目录",
   "title_sidebar": "大纲",
   "toc_cell": true,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
